package com.cps.platform.client.hbase;

import java.util.List;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.log4j.Logger;
import org.springframework.http.MediaType;

import com.cps.platform.client.Constant;
import com.cps.platform.client.config.ConfigClient;
import com.cps.platform.core.hbase.FilterBean;
import com.cps.platform.core.hbase.HbasePut;
import com.cps.platform.core.json.JsonParserUtil;

/**
 * <p>
 * 文件名称: HbaseClient.java
 * </p>
 * <p>
 * 文件描述: HBASE数据存储客户端
 * <p>
 * 版权所有: 版权所有(C)2011
 * </p>
 * <p>
 * 公 司: 联通系统集成有限公司
 * </p>
 * <p>
 * 内容摘要: 对HBASE数据存储服务的客户端封装，
 * 		如果需要使用服务则将相关类或JAR包直接拷贝到工程中即可。
 * </p>
 * <p>
 * 其他说明: 
 * </p>
 * <p>
 * 完成日期：2012-12-5
 * </p>
 * 
 * @author ChenZhang
 */
public class HbaseClient {

	private static Logger log = Logger.getLogger(HbaseClient.class);
	private String baseAddress;
	
	public HbaseClient() {
		ConfigClient configClient = new ConfigClient();
		this.baseAddress =  (String) configClient.getServiceBaseAddress().getMap().get("hbase");
	}

	/**
	 * 创建表
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param tablename
	 * @return
	 */
	public boolean createTable(String appId, String authToken, String tenantId, String tablename) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/create/{appId}/{authToken}/{tenantId}/{tablename}";
				
		Response response = webClient.path(serviceAddress,appId,authToken,tenantId,tablename)
			.accept(MediaType.APPLICATION_JSON_VALUE).get();
		
		int status = response.getStatus();
		log.info("[Client] tenantId:"+tenantId+", tablename:"+tablename+", status:"+ status);
		
		return Constant.OK==status?true:false;
	}
	
	/**
	 * 删除表
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param tablename
	 * @return
	 */
	public boolean dropTable(String appId, String authToken, String tenantId, String tablename){
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/drop/{appId}/{authToken}/{tenantId}/{tablename}";
		
		Response response = webClient.path(serviceAddress,appId,authToken,tenantId,tablename)
			.accept(MediaType.APPLICATION_JSON_VALUE).delete();
		
		int status = response.getStatus();
		log.info("[Client] tenantId:"+tenantId+", tablename:"+tablename+", status:"+ status);
		
		return Constant.OK==status?true:false;
	}
	
	/**
	 * 添加或更新数据<br/>
	 * <p>如果hbasePut的rowKey不存在则添加，否则执行修改。</p>
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param hbasePut
	 * @return
	 */
	public HbasePut saveData(String appId, String authToken, String tenantId, HbasePut hbasePut) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/save/{appId}/{authToken}/{tenantId}";
		String hbasePutJson = webClient.path(serviceAddress,appId,authToken,tenantId)
				.accept(MediaType.APPLICATION_JSON_VALUE).header("Content-Type", "application/json")
				.invoke(HttpMethod.POST, JsonParserUtil.getJsonFromObject(hbasePut), String.class);
		
		log.info("[Client] tenantId:"+tenantId+", request:"+JsonParserUtil.getJsonFromObject(hbasePut));
		log.info("[Client] tenantId:"+tenantId+", response:"+hbasePutJson);
		
		return (HbasePut) JsonParserUtil.getObjectFromJson(hbasePutJson, HbasePut.class);
	}
	
	/**
	 * 删除
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param tablename
	 * @param rowKeys
	 * @return
	 */
	public boolean removeDatas(String appId, String authToken, String tenantId, 
			String tablename, List<String> rowKeys) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/multi-remove/{appId}/{authToken}/{tenantId}/{tablename}/{rowkeysJson}";
		
		String rowKeysJson = JsonParserUtil.getJsonFromObject(rowKeys);
		Response response = webClient.path(serviceAddress,appId,authToken,tenantId,tablename,rowKeysJson)
			.accept(MediaType.APPLICATION_JSON_VALUE).delete();
		
		int status = response.getStatus();
		log.info("[Client] tenantId:"+tenantId+", tablename:"+tablename+", rowkeys:"+rowKeysJson+", status:"+ status);
		
		return Constant.OK==status?true:false;
	}
	
	/**
	 * 删除
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param tablename
	 * @param rowKey
	 * @return
	 */
	public boolean removeData(String appId, String authToken, String tenantId, 
			String tablename, String rowKey) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/remove/{appId}/{authToken}/{tenantId}/{tablename}/{rowkeysJson}";
		
		Response response = webClient.path(serviceAddress,appId,authToken,tenantId,tablename,rowKey)
			.accept(MediaType.APPLICATION_JSON_VALUE).delete();
		
		int status = response.getStatus();
		log.info("[Client] tenantId:"+tenantId+", tablename:"+tablename+",rowkeys:"+rowKey+",  status:"+ status);
		
		return Constant.OK==status?true:false;
	}
	
	/**
	 * 获取一个数据对象
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param tableName
	 * @param rowKey
	 * @return
	 */
	public HbasePut getData(String appId, String authToken, String tenantId, 
			String tableName, String rowKey) {
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/get/{appId}/{authToken}/{tenantId}/{tablename}/{rowkey}";
		
		String putsJson = webClient.path(serviceAddress,appId,authToken,tenantId,tableName,rowKey)
			.accept(MediaType.APPLICATION_JSON_VALUE).header("Content-Type", "application/json")
			.invoke(HttpMethod.GET,"",String.class);
		
		log.info("[Client] tenantId:"+tenantId+", tableName:"+tableName+", response:"+putsJson);

		return (HbasePut) JsonParserUtil.getObjectFromJson(putsJson, HbasePut.class);
	}
	
	/**
	 * 查询表中的所有数据
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param tableName
	 * @return
	 */
	public List<HbasePut> queryAllData(String appId, String authToken, String tenantId, 
			String tableName){
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/query-all/{appId}/{authToken}/{tenantId}/{tablename}";
		
		String putsJson = webClient.path(serviceAddress,appId,authToken,tenantId,tableName)
			.accept(MediaType.APPLICATION_JSON_VALUE).header("Content-Type", "application/json")
			.invoke(HttpMethod.GET,"",String.class);
		
		log.info("[Client] tenantId:"+tenantId+", tableName:"+tableName+", response:"+putsJson);
		
		return JsonParserUtil.getObjectsFromJson(putsJson, HbasePut.class);
	}
	
	/**
	 * 根据过滤器查询数据<br/>
	 * <p>
	 * 	1、过滤条件只支持AND运算;
	 *  2、ROWKEY的命名应该遵循统一命名规范，即key:value格式，各个元素之间使用"_"分割，第一个元素为long的日期格式（无KEY）。
	 * </p>
	 * 
	 * @param appId
	 * @param authToken
	 * @param tenantId
	 * @param tableName
	 * @param filterBeans
	 * @return
	 */
	public List<HbasePut> queryMoreData(String appId, String authToken, String tenantId, 
			String tableName, List<FilterBean> filterBeans){
		WebClient webClient = WebClient.create(baseAddress);
		String serviceAddress = "/hbaseService/query-more/{appId}/{authToken}/{tenantId}/{tablename}/{filterJson}";
		
		String filterJson = JsonParserUtil.getJsonFromObject(filterBeans);
		String putsJson = webClient.path(serviceAddress,appId,authToken,tenantId,tableName,filterJson)
			.accept(MediaType.APPLICATION_JSON_VALUE).header("Content-Type", "application/json")
			.invoke(HttpMethod.GET,"",String.class);
		
		log.info("[Client] tenantId:"+tenantId+", tableName:"+tableName+", request:"+filterJson);
		log.info("[Client] tenantId:"+tenantId+", tableName:"+tableName+", response:"+putsJson);
		
		return JsonParserUtil.getObjectsFromJson(putsJson, HbasePut.class);
	}
	
}
